/**
 * @Title: DJobServiceImpl.java
 * @Package com.kaver.capital.service.common.component.djob.impl
 * @author roc
 * @date 2020年8月14日 下午4:13:00
 * @version V1.0
 * @copyright kaver
 */
package com.kaver.job.core.djob;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionSynchronizationAdapter;
import org.springframework.transaction.support.TransactionSynchronizationManager;

import com.kaver.job.core.biz.model.JobInfo;

/**
 * @ClassName: DJobServiceImpl
 * @Description: 动态job
 * @author roc
 * @date 2020年8月14日 下午4:13:00
 */
public class DJobServiceImpl implements DJobService {
    private static final Logger logger = LoggerFactory.getLogger(DJobServiceImpl.class);
    @Autowired
    DynamicJob dynamicJob;
    
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void pubOneCountJob(DJobEnum enums, String serialNo, String param) {
        if (TransactionSynchronizationManager.isSynchronizationActive()) {
            TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
                @Override
                public void afterCommit() {
                    doPubOneCountJob(enums, serialNo, param);
                }
            });
        } else {
            doPubOneCountJob(enums, serialNo, param);
        }
    }

    private void doPubOneCountJob(DJobEnum enums, String serialNo, String param) {
        boolean flag = dynamicJob.addOneCountJob(enums.getJobName(), serialNo, param);
        if (!flag) {
            logger.error("动态job(OneCount)发布失败【{}】, 参数【{}】", enums.getDesc(), param);
        }
    }
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void pubOneCountJob(DJobEnum enums, String serialNo, String param, long delay) {
        if (TransactionSynchronizationManager.isSynchronizationActive()) {
            TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
                @Override
                public void afterCommit() {
                    doPubOneCountJob(enums, serialNo, param, delay);
                }
            });
        } else {
            doPubOneCountJob(enums, serialNo, param, delay);
        }
    }

    private void doPubOneCountJob(DJobEnum enums, String serialNo, String param, long delay) {
        boolean flag = dynamicJob.addOneCountJob(enums.getJobName(), serialNo, param, delay);
        if (!flag) {
            logger.error("动态job(OneCountDelay)发布失败【{}】, 参数【{}】", enums.getDesc(), param);
        }
    }
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void pubRateJob(DJobEnum enums, String serialNo, String param) {
        if (TransactionSynchronizationManager.isSynchronizationActive()) {
            TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
                @Override
                public void afterCommit() {
                    doPubRateJob(enums, serialNo, param);
                }
            });
        } else {
            doPubRateJob(enums, serialNo, param);
        }
    }

    private void doPubRateJob(DJobEnum enums, String serialNo, String param) {
        boolean flag = dynamicJob.addRateJob(enums.getJobName(), enums.getCron(), serialNo, param);
        if (!flag) {
            logger.error("动态job(Rate)发布失败【{}】, 参数【{}】", enums.getDesc(), param);
        }
    }
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void pubRateJob(DJobEnum enums, String serialNo, String param, long delay) {
        if (TransactionSynchronizationManager.isSynchronizationActive()) {
            TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
                @Override
                public void afterCommit() {
                    doPubRateJob(enums, serialNo, param, delay);
                }
            });
        } else {
            doPubRateJob(enums, serialNo, param, delay);
        }
    }

    private void doPubRateJob(DJobEnum enums, String serialNo, String param, long delay) {
        boolean flag = dynamicJob.addRateJob(enums.getJobName(), enums.getCron(), serialNo, param, delay);
        if (!flag) {
            logger.error("动态job(RateDelay)发布失败【{}】, 参数【{}】", enums.getDesc(), param);
        }
    }

    @Override
    public void stopJob(DJobEnum enums, String serialNo) {
        JobInfo jobInfo = dynamicJob.queryJob(serialNo, enums.getJobName());
        if (jobInfo != null) {
            int triggerStatus = jobInfo.getTriggerStatus();
            if (triggerStatus == 1) {
                boolean flag = dynamicJob.stopJob(serialNo, enums.getJobName());
                if (!flag) {
                    logger.error("动态job(Stop)停止失败【{}】, 参数【{}】", enums.getDesc(), serialNo);
                }
            }
        }
    }

    @Override
    public long count(DJobEnum enums, String serialNo) {
        try {
            JobInfo jobInfo = dynamicJob.queryJob(serialNo, enums.getJobName());
            if (jobInfo != null) {
                return jobInfo.getExecutorCount();
            }
        } catch (Exception e) {
            logger.error("动态job(count)失败【{}】, 参数【{}】", enums.getDesc(), serialNo);
        }
        
        return -1;
    }

}
